package com.briup.security.web;

import com.briup.security.service.IImageCode;
import com.briup.security.util.ImageCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/image")
public class ImageController {

    @Autowired
    private IImageCode imageCode;

    @GetMapping("/code")
    public void imageCode(HttpServletRequest request, HttpSession session, HttpServletResponse response) throws IOException {
        ImageCode code = imageCode.createCode();
        session.setAttribute("SESSION_IMAGE_CODE",code);
        response.setContentType("image/jpeg");
        ImageIO.write(code.getImage(),"JPEG",response.getOutputStream());

    }
}
